//
//  StepView.m
//  ELife
//
//  Created by xiaozhi on 11-5-24.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "StepView.h"


@implementation StepView
NSInteger _stepIndex;
static NSMutableArray *focusImageNames;
static NSMutableArray *unfocusImageNames;

- (id)createStepViewWithStep:(NSInteger)step
{
    _stepIndex = step;
    CGRect frame = CGRectMake(10, 5, 300, 50);
    self = [super initWithFrame:frame];
    if (self) {
        [self initStepView:frame];
    }
    return self;
}

- (void)initImageNames
{
    focusImageNames = [[NSMutableArray alloc]initWithCapacity:MAX_STEP];
    unfocusImageNames = [[NSMutableArray alloc]initWithCapacity:MAX_STEP];
    
    for (unsigned int index = 0; index < MAX_STEP; index++) {
        NSString *focusName = [[NSString alloc] 
                               initWithFormat:@"%@%d", @"step_focus_", index + 1];
        [focusImageNames addObject:focusName];
        [focusName release];
        
        NSString *unfocusName = [[NSString alloc] 
                                 initWithFormat:@"%@%d", @"step_unfocus_", index + 1];
        [unfocusImageNames addObject:unfocusName];
        [unfocusName release];
    }

}

- (void)initStepView:(CGRect)frame
{
    self.layer.cornerRadius = 8;
    self.layer.masksToBounds = YES;
    self.backgroundColor = [[UIColor alloc] 
                            initWithRed:255 green:255 blue:255 alpha:1];
    self.frame = frame;
    
    [self initImageNames];
    
    CGFloat parentX = frame.origin.x;
    CGFloat parentY = frame.origin.y;
    CGFloat imageX = parentX;
    for (unsigned int index = 0; index < MAX_STEP; index++) {
        UIImage *image;
        
        // set focus
        if (index == _stepIndex) {
            image = [UIImage imageNamed:[focusImageNames objectAtIndex:index]];
        } else {
            image = [UIImage imageNamed:[unfocusImageNames objectAtIndex:index]];
        }
        
        CGFloat imageWid = image.size.width;
        CGFloat imageHei = image.size.height;
        
        UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
        imageView.frame = CGRectMake(imageX, parentY, imageWid, imageHei);
        
        imageX += imageWid;
        
        [self addSubview:imageView];
        [imageView release];
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

- (void)dealloc
{
    [super dealloc];
}

@end
